package gui.cen.home;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import javax.swing.BorderFactory;
import javax.swing.JPanel;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Ticker extends JPanel implements Runnable{
    private boolean gogo;
    private String message;
    public Ticker(String m){
        super(new FlowLayout());
        this.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
        this.message = m;
        this.setBackground(Color.WHITE);
        gogo = true;
        this.start();
    }

    public void start(){
        Thread th = new Thread(this);
        th.start();
    }

    private String msg = "";
    public void run() {
        int counter = 0;
        while(gogo){
            if(counter < message.length()+1){msg = message.substring(0, counter++);}
            else{counter = 0; msg = "";}
            this.repaint();
            try{Thread.sleep(100);}
            catch(InterruptedException ex){}
        }
    }

    public void setGoGo(boolean b){gogo = b;}

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.black);
        drawMessage(g);
    }

    private Graphics2D bufferGraphics = null;
    private Image offScreen = null;

    //use card layout and panels here to do movie!!
    private void drawMessage(Graphics g){
        final int imageHeight = 16;
        final int imageWidth = this.getWidth();
        this.setLayout(new FlowLayout(){public Dimension preferredLayoutSize(Container cont) {return (new Dimension(imageWidth, imageHeight));}});
        offScreen = this.createImage(imageWidth, imageHeight);
        bufferGraphics = (Graphics2D)offScreen.getGraphics();
        bufferGraphics.setColor(new Color(102, 102, 102));
        bufferGraphics.setFont(new Font("Tahoma", Font.BOLD, 12));
        bufferGraphics.drawString(msg, 5, 10);
        g.drawImage(offScreen, 0, 0, imageWidth, imageHeight, this);
    }
}
